import wx


class ControlPanel(wx.Panel):
    def __init__(self, parent, config, data_loader):
        super().__init__(parent)
        self.config = config
        self.data_loader = data_loader
        self.main_frame = parent.GetTopLevelParent()  # 获取顶层窗口引用
        self.init_ui()

    def init_ui(self):
        self.SetBackgroundColour(wx.Colour(220, 220, 220))

        main_sizer = wx.BoxSizer(wx.VERTICAL)

        # 筛选控制
        filter_sizer = wx.BoxSizer(wx.HORIZONTAL)
        filter_sizer.Add(wx.StaticText(self, label="筛选:"), 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)

        self.filter_map = {"全部": "all", "已掌握": "learned", "未掌握": "unlearned"}
        self.reverse_filter_map = {v: k for k, v in self.filter_map.items()}
        self.filter_choice = wx.Choice(self, choices=list(self.filter_map.keys()))

        self.filter_choice.Bind(wx.EVT_CHOICE, self.on_filter_change)
        filter_sizer.Add(self.filter_choice, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)

        # 获取配置中的筛选状态，并找到对应的中文选项
        config_filter = self.config.get('filter_status', 'all')
        default_selection = self.reverse_filter_map.get(config_filter, "全部")
        self.filter_choice.SetStringSelection(default_selection)

        # 显示全部词义按钮
        self.show_all_btn = wx.Button(self, label="显示全部词义")
        self.show_all_btn.Bind(wx.EVT_BUTTON, self.on_show_all_definitions)
        filter_sizer.Add(self.show_all_btn, 0, wx.ALIGN_CENTER_VERTICAL)

        main_sizer.Add(filter_sizer, 0, wx.EXPAND | wx.ALL, 10)

        # 分页控制
        page_sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.first_page_btn = wx.Button(self, label="首页")
        self.first_page_btn.Bind(wx.EVT_BUTTON, lambda e: self.on_page_change(1))
        page_sizer.Add(self.first_page_btn, 0, wx.RIGHT, 5)

        self.prev_page_btn = wx.Button(self, label="上页")
        self.prev_page_btn.Bind(wx.EVT_BUTTON, self.on_prev_page)
        page_sizer.Add(self.prev_page_btn, 0, wx.RIGHT, 5)

        self.page_text = wx.StaticText(self, label="1")
        page_sizer.Add(self.page_text, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)

        self.page_input = wx.TextCtrl(self, size=(40, -1))
        page_sizer.Add(self.page_input, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)

        self.go_page_btn = wx.Button(self, label="跳转")
        self.go_page_btn.Bind(wx.EVT_BUTTON, self.on_go_page)
        page_sizer.Add(self.go_page_btn, 0, wx.RIGHT, 5)

        self.next_page_btn = wx.Button(self, label="下页")
        self.next_page_btn.Bind(wx.EVT_BUTTON, self.on_next_page)
        page_sizer.Add(self.next_page_btn, 0, wx.RIGHT, 5)

        self.last_page_btn = wx.Button(self, label="尾页")
        self.last_page_btn.Bind(wx.EVT_BUTTON, self.on_last_page)
        page_sizer.Add(self.last_page_btn, 0)

        main_sizer.Add(page_sizer, 0, wx.ALIGN_CENTER | wx.BOTTOM, 10)

        self.SetSizer(main_sizer)

    def update_page_info(self, current_page, total_pages):
        self.page_text.SetLabel(f"{current_page}/{total_pages}")
        self.page_input.SetValue("")

        # 更新按钮状态
        self.first_page_btn.Enable(current_page > 1)
        self.prev_page_btn.Enable(current_page > 1)
        self.next_page_btn.Enable(current_page < total_pages)
        self.last_page_btn.Enable(current_page < total_pages)

        # 更新配置
        self.config.set('current_page', current_page)

    def on_filter_change(self, event):
        filter_map = {"全部": "all", "已掌握": "learned", "未掌握": "unlearned"}
        filter_status = filter_map[self.filter_choice.GetStringSelection()]
        self.config.set('filter_status', filter_status)
        self.main_frame.load_page()

    def on_show_all_definitions(self, event):
        # 切换显示状态
        show_all = not self.main_frame.show_all_definitions
        self.main_frame.show_all_definitions = show_all
        self.config.set('show_all_definitions', show_all)
        self.main_frame.update_display_mode(show_all)
        self.show_all_btn.SetLabel("隐藏全部词义" if show_all else "显示全部词义")

    def on_page_change(self, page):
        total_pages = self.data_loader.get_total_pages(filter_status=self.config.get('filter_status', 'all'))
        if 1 <= page <= total_pages:
            self.main_frame.load_page(page)

    def on_prev_page(self, event):
        current_page = self.config.get('current_page', 1)
        self.on_page_change(current_page - 1)

    def on_next_page(self, event):
        current_page = self.config.get('current_page', 1)
        self.on_page_change(current_page + 1)

    def on_go_page(self, event):
        try:
            page = int(self.page_input.GetValue())
            self.on_page_change(page)
        except ValueError:
            wx.MessageBox("请输入有效的页码", "错误", wx.OK | wx.ICON_ERROR)

    def on_last_page(self, event):
        total_pages = self.data_loader.get_total_pages(filter_status=self.config.get('filter_status', 'all'))
        self.on_page_change(total_pages)